package apka.SMSBillet;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import apka.SMSBillet.Activities.ConfirmTicket;
import apka.SMSBillet.Activities.DisplayMessage;

public class SMSBroadcastReceiver extends BroadcastReceiver {

        private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
        private static final String TAG = "SMSBroadcastReceiver";
        private static final String CONFIRM_CONTAINS ="Du har bestilt ";
        private static final String DISPLAY_CONTAINS ="Afrejsezone:";
        private static final String SENDER = "1271";
       
        @Override
        public void onReceive(Context context, Intent intent) {
             Log.i(TAG,intent.getAction());
             	if (intent.getAction().equals(SMS_RECEIVED)){
                	Log.i(TAG, "Is in if sentence?");
                    Bundle bundle = intent.getExtras();
                    if (bundle != null) {
                        Object[] pdus = (Object[])bundle.get("pdus");
                        final SmsMessage[] messages = new SmsMessage[pdus.length];
                        for (int i = 0; i < pdus.length; i++) {
                            messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        }
                        String finalMessage = compileSms(messages);
                        if (messages.length > -1 
                        		&& messages[0].getOriginatingAddress().equals(SENDER) 
                        		&& messages[0].getDisplayMessageBody().contains(CONFIRM_CONTAINS)) {
                            Log.i(TAG, "From: "+ messages[0].getDisplayOriginatingAddress()+" Wrote: " +messages[0].getMessageBody());
                            Intent startIntent = new Intent(context.getApplicationContext(), ConfirmTicket.class);
                            startIntent.putExtra("From", messages[0].getDisplayOriginatingAddress());
                            startIntent.putExtra("Message", finalMessage);
                            startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(startIntent);
                        }
                        else if(messages.length > -1 
                        		&& messages[0].getOriginatingAddress().equals(SENDER) 
                        		&& messages[0].getDisplayMessageBody().contains(DISPLAY_CONTAINS)){
                            Intent startIntent = new Intent(context.getApplicationContext(), DisplayMessage.class);
                            startIntent.putExtra("From", messages[0].getDisplayOriginatingAddress());
                            startIntent.putExtra("Message", finalMessage);
                            startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(startIntent);                           
                        }
                    }
                }
           }
        private String compileSms(SmsMessage[] smsArray){
        	String finalMessage = "";
        	for (int i = 0; i < smsArray.length; i++){
        		finalMessage +=smsArray[i].getMessageBody();;
        	}
        	
        	return finalMessage;
        }
        
    }